package com.jiangtour.controller;

import com.jiangtour.dataobject.SiteInfo;
import com.jiangtour.dto.ExcelData;
import com.jiangtour.dto.OrderDTO;
import com.jiangtour.dto.QueryOrderDTO;
import com.jiangtour.enums.ConfigureEnum;
import com.jiangtour.enums.SendFlagEnum;
import com.jiangtour.service.CategoryService;
import com.jiangtour.service.ConfigureCategoryService;
import com.jiangtour.service.OrderService;
import com.jiangtour.service.SiteInfoService;
import com.jiangtour.utils.ExportExcelUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 * @Title: SiteOrderController
 * @ProjectName site
 * @Description: 后台订单
 * @date 2018/12/28/02814:50
 */
@Controller
@RequestMapping("/order")
@Slf4j
public class SiteOrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private SiteInfoService siteInfoService;
    @Autowired
    private ConfigureCategoryService configureCategoryService;
    @Autowired
    private CategoryService categoryService;
    /**
     * 订单列表
     * @param page 第几页, 从1页开始
     * @param size 一页有多少条数据
     * @return
     */
    @GetMapping("/list")
    public ModelAndView list(@RequestParam(value = "page", defaultValue = "1") Integer page,
                             @RequestParam(value = "size", defaultValue = "10") Integer size,
                              QueryOrderDTO queryOrderDTO,
                             HttpServletRequest httpServletRequest,
                             Map<String, Object> map) {
        PageRequest request =PageRequest.of(page - 1, size);

        // 归属
        SiteInfo currentAccount = siteInfoService.findCurrentAccount(httpServletRequest);
        if(currentAccount !=null ){
            queryOrderDTO.setAscriptionId(currentAccount.getId());
        }

        Page<OrderDTO> orderDTOPage = orderService.findByList(request,queryOrderDTO);
        map.put("orderDTOPage", orderDTOPage);
        map.put("currentPage", page);
        map.put("size", size);
        map.put("pointList",configureCategoryService.findList(currentAccount.getId(), ConfigureEnum.PICKING_POINT.getCode()));
        map.put("specList",categoryService.findCategoryAscription(currentAccount.getId()));
        map.put("expressCompany",configureCategoryService.findList(currentAccount.getId(), ConfigureEnum.EXPRESS.getCode()));
        map.put("queryOrderDTO", queryOrderDTO);
        if(queryOrderDTO.getType()==SendFlagEnum.COLLECT.getCode()){
            return new ModelAndView("order/list", map);
        }
        return new ModelAndView("order/sendlist", map);
    }

    @GetMapping("/export")
    public void excel(HttpServletResponse response,
                              QueryOrderDTO queryOrderDTO,
                              HttpServletRequest httpServletRequest,
                              Map<String, Object> map) throws Exception {
        // 归属
        SiteInfo currentAccount = siteInfoService.findCurrentAccount(httpServletRequest);
        if(currentAccount !=null ){
            queryOrderDTO.setAscriptionId(currentAccount.getId());
        }

        ExcelData data = new ExcelData();
        List<List<Object>> rows = new ArrayList();
        data.setName("hello");
        List<String> titles = new ArrayList();

        if(queryOrderDTO.getType().equals(SendFlagEnum.COLLECT.getCode())){
            titles.add("下单时间");
            titles.add("学校");
            titles.add("联系方式");
            titles.add("姓名");
            titles.add("取件点");
            titles.add("取件信息");
            titles.add("备注");
            titles.add("规格");

            data.setTitles(titles);

            List<OrderDTO>  orderDTOList= orderService.findAllExcel(queryOrderDTO);
            for (int i=0;i<orderDTOList.size();i++){
                List<Object>  row = new ArrayList<>();
                row.add(orderDTOList.get(i).getCreateTime());
                row.add(orderDTOList.get(i).getAscriptionId());
                row.add(orderDTOList.get(i).getBuyerPhone());
                row.add(orderDTOList.get(i).getBuyerName());
                row.add(orderDTOList.get(i).getPickUpPartsName());
                row.add(orderDTOList.get(i).getAdditionalInfo());
                row.add(orderDTOList.get(i).getRemarks());
                row.add(orderDTOList.get(i).getSpecName());

                rows.add(row);

            }
        }else {
            titles.add("下单时间");
            titles.add("学校");
            titles.add("姓名");
            titles.add("电话");
            titles.add("收件人");
            titles.add("收件人电话");
            titles.add("收件人地址");
            titles.add("快递公司");
            titles.add("备注");

            data.setTitles(titles);

            List<OrderDTO>  orderDTOList= orderService.findAllExcel(queryOrderDTO);
            for (int i=0;i<orderDTOList.size();i++){
                List<Object>  row = new ArrayList<>();
                row.add(orderDTOList.get(i).getCreateTime());
                row.add(orderDTOList.get(i).getAscriptionId());
                row.add(orderDTOList.get(i).getBuyerName());
                row.add(orderDTOList.get(i).getBuyerPhone());
                row.add(orderDTOList.get(i).getAddressee());
                row.add(orderDTOList.get(i).getAddresseePhone());
                row.add(orderDTOList.get(i).getAddresseeAddress());
                row.add(orderDTOList.get(i).getExpressName());
                row.add(orderDTOList.get(i).getRemarks());
                rows.add(row);
            }
        }

        data.setRows(rows);
        ExportExcelUtil.exportExcel(response,"export.xlsx",data);

    }


}
